package com.srcbox.file.util

import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.net.Uri
import android.widget.Toast
import com.alibaba.fastjson.JSON
import java.io.InputStream
import java.net.URL
import javax.net.ssl.HttpsURLConnection

class AppSendCon(private val activity: Activity) {

    fun appNotice(link: String) {
        Thread {
            val url = URL(link)
            val httpC = url.openConnection() as HttpsURLConnection
            httpC.connectTimeout = 5000
            httpC.readTimeout = 5000
            httpC.requestMethod = "GET"
            if(httpC.responseCode == 200){
                val alertDialog = AlertDialog.Builder(activity)
                    .setTitle("公告")
                    .setMessage(in2Str(httpC.inputStream))
                alertDialog.show()
            }
        }
    }

    fun updateApp(link: String) {
        Thread {
            try {
                val url = URL(link)
                val httpC = url.openConnection() as HttpsURLConnection
                httpC.connectTimeout = 5000
                httpC.readTimeout = 5000
                httpC.requestMethod = "GET"
                if (httpC.responseCode == 200) {
                    val content = in2Str(httpC.inputStream)
                    val jsonObject = JSON.parseObject(content)
                    val version = jsonObject["version"].toString().toInt()
                    val updateCon = jsonObject["updateCon"]
                    val updateLink = jsonObject["updateLink"]
                    val currAppVersion = activity.applicationContext.packageManager.getPackageInfo(
                        activity.packageName,
                        0
                    ).versionName.toInt()
                    if (version > currAppVersion) {
                        activity.runOnUiThread {
                            val alertDialog = AlertDialog.Builder(activity)
                                .setTitle("有新版本！")
                                .setMessage(updateCon.toString())
                                .setNegativeButton(
                                    "更新"
                                ) { _, _ ->
                                    goBrowser(updateLink.toString())
                                }
                            alertDialog.show()
                        }
                    }
                } else {
                    activity.runOnUiThread {
                        toast("网络错误")
                    }
                }
            } catch (e: Exception) {
                toast(e.message)
            }
        }
    }

    private fun toast(string: String?) {
        Toast.makeText(activity, string, Toast.LENGTH_LONG).show()
    }

    private fun in2Str(insStream: InputStream): String {
        var str = ""
        var len = 0
        val byteBuff = ByteArray(524)
        while ((insStream.read(byteBuff).apply { len = this }) != -1) {
            str += String(byteBuff, 0, len)
        }
        return str
    }

    private fun goBrowser(link: String) {
        val intent = Intent()
        intent.action = "android.intent.action.VIEW"
        val uri = Uri.parse(link)
        intent.data = uri
        activity.startActivity(intent)
    }
}